using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Triton.Common
{
	public class AttributeFinder<T> where T : Attribute
	{
		[CompilerGenerated]
		private List<Tuple<MemberInfo, List<T>>> list_0;

		[CompilerGenerated]
		private List<Tuple<Type, List<T>>> list_1;

		public List<Tuple<MemberInfo, List<T>>> AttributedMembers
		{
			[CompilerGenerated]
			get
			{
				return list_0;
			}
			[CompilerGenerated]
			set
			{
				list_0 = value;
			}
		}

		public List<Tuple<Type, List<T>>> AttributedObjects
		{
			[CompilerGenerated]
			get
			{
				return list_1;
			}
			[CompilerGenerated]
			set
			{
				list_1 = value;
			}
		}

		public AttributeFinder(bool publicOnly = true, bool includeGacAssemblies = false)
		{
			AttributedMembers = new List<Tuple<MemberInfo, List<T>>>();
			AttributedObjects = new List<Tuple<Type, List<T>>>();
			AttributeUsageAttribute customAttribute = typeof(T).GetCustomAttribute<AttributeUsageAttribute>();
			bool flag = customAttribute.ValidOn.HasFlag(AttributeTargets.Class);
			bool flag2 = customAttribute.ValidOn.HasFlag(AttributeTargets.Struct);
			bool flag3 = customAttribute.ValidOn.HasFlag(AttributeTargets.Enum);
			bool flag4 = (customAttribute.ValidOn & (AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event)) != 0;
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				if (!includeGacAssemblies && assembly.GlobalAssemblyCache)
				{
					continue;
				}
				Type[] types = assembly.GetTypes();
				foreach (Type type in types)
				{
					if ((flag && !type.IsClass) || (flag3 && !type.IsEnum) || (flag2 && !type.IsValueType) || (!publicOnly && !type.IsPublic))
					{
						continue;
					}
					List<T> list = type.GetCustomAttributes<T>().ToList();
					if (list.Count != 0)
					{
						AttributedObjects.Add(new Tuple<Type, List<T>>(type, list));
					}
					if (!flag4)
					{
						continue;
					}
					MemberInfo[] members = type.GetMembers();
					foreach (MemberInfo memberInfo in members)
					{
						List<T> list2 = memberInfo.GetCustomAttributes<T>().ToList();
						if (list2.Count != 0)
						{
							AttributedMembers.Add(new Tuple<MemberInfo, List<T>>(memberInfo, list2));
						}
					}
				}
			}
		}
	}
}
